<template>
	<view>
		<u-popup v-model="addressPopup" mode="bottom" border-radius="20">
			<view class="addressPopup">
				<view class="popupHeadView">
					<text class="popupTitle">选择地址</text>
					<image class="popupCancel" src="/static/icons/close3.png" mode="aspectFit" @click="close"></image>
				</view>
				<scroll-view class="scrollView" scroll-y>
					<view class="itemAddressView" v-for="(item,index) in addressList" :key="index"
						@click="onSelected(item)">
						<image class="checkIcon" src="/static/icons/selected.png" v-if="item.selected"></image>
						<image class="checkIcon" src="/static/icons/unselected2.png" v-else></image>
						<view class="addresInfo">
							<view class="itemAddressView">
								<view class="addrListView" style="flex: 1;">
									<view class="addrInfoView">
										<text class="addressTitle2">{{item.name}}</text>
										<text class="addrPhone">{{item.phone}}</text>
									</view>
									<view class="itemAddress">
										<text class="defaultTitle" v-if="item.default == 1">默认</text>
										<view style="flex: 1;">{{item.address}}</view>
									</view>
								</view>
								<image class="checkIcon" src="/static/icons/edit.png" @click.stop="onEditAddress(item)">
								</image>
							</view>
							<view class="line"></view>
						</view>
					</view>
				</scroll-view>
				<view class="newAddress" @click="onAddAddress()">+ 添加地址</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: {
			addressList: {
				type: Array // 定义参数的类型
			}
		},
		name: "address-popup",
		data() {
			return {
				addressPopup: false,
			};
		},

		methods: {
			open() {
				this.addressPopup = true;

			},
			close() {
				this.addressPopup = false;
			},

			onSelected(item) {
				this.addressList.forEach((item, index) => {
					item.selected = false;
				});
				item.selected = true;
				this.$forceUpdate();
				this.$emit('select', item);
				this.addressPopup = false;
			},
			onEditAddress(item) {
				this.$emit('edit', item);
				this.addressPopup = false;
			},
			onAddAddress() {
				this.$emit('add', true);
				this.addressPopup = false;
			}
		}
	}
</script>

<style scoped>
	.addressPopup {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 850rpx;
	}

	.popupHeadView {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		height: 120rpx;
		position: relative;
	}

	.popupTitle {
		font-size: 30rpx;
		color: #000;
		font-weight: bold;
	}

	.popupCancel {
		width: 30rpx;
		height: 30rpx;
		padding: 10rpx;
		position: absolute;
		right: 20rpx;
		z-index: 100;
	}

	.scrollView {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 600rpx;
	}

	.addresInfo {
		flex: 1;
		display: flex;
		flex-direction: column;
		padding-top: 20rpx;
	}

	.itemAddressView {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.checkIcon {
		width: 36rpx;
		height: 36rpx;
		margin-left: 25rpx;
		margin-right: 25rpx;
	}

	.addrListView {
		flex: 1;
		display: flex;
		flex-direction: column;
		border-radius: 10rpx;
	}

	.addrInfoView {
		display: flex;
		flex-direction: row;
	}

	.addressTitle2 {
		flex: 1;
		font-size: 30rpx;
		font-weight: bold;
		color: #333333;
	}

	.addrPhone {
		font-size: 28rpx;
		font-weight: 400;
		color: #333333;
	}

	.itemAddress {
		display: flex;
		flex-direction: row;
		font-size: 28rpx;
		font-weight: 400;
		color: #333333;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}

	.defaultTitle {
		background: #FFDED4;
		border-radius: 4rpx;
		height: 40rpx;
		font-size: 24rpx;
		margin-right: 10rpx;
		color: #FF5A5A;
		margin-top: 5rpx;
	}

	.newAddress {
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		background: #F6632D;
		border-radius: 45rpx;
		font-size: 30rpx;
		font-weight: bold;
		color: #FFFFFF;
		margin: 20rpx 30rpx;
	}

	.line {
		height: 1px;
		background: #f5f5f5;
	}
</style>